package com.dv.shell.controller;

import com.dv.shell.form.OrderForm;
import com.dv.shell.converter.OrderForm2OrderDTOConverter;
import com.dv.shell.dto.OrderDTO;
import com.dv.shell.enums.ResultEnum;
import com.dv.shell.exception.SellException;
import com.dv.shell.service.BuyerService;
import com.dv.shell.service.OrderService;
import com.dv.shell.vo.ResultVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.loader.custom.Return;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author DavidLoman
 * @create 2021/10/7 2:24
 */
@Slf4j
@RestController
@RequestMapping("/buyer/order")
@RequiredArgsConstructor
public class BuyerOrderController {

    private final OrderService orderService;
    private final BuyerService buyerService;
    /**
     * 创建订单
     * @param orderForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public ResultVo<Map<String, String>> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            log.error("[创建订单] 参数不正确,orderForm={}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldErrors());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("[创建订单]购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO result = orderService.create(orderDTO);
        System.out.println(result);
        HashMap<String, String> map = new HashMap<>();
        map.put("orderId",result.getOrderId());
        return ResultVo.success(map);
    }

    /**
     * 查询订单列表
     * @param openid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public ResultVo<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(openid)){
            log.error("[查询订单列表] openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, request);
        return ResultVo.success(orderDTOPage.getContent());
    }

    /**
     * 查询订单详情
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVo<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderid")String orderId){
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultVo.success(orderDTO);
    }


    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderid")String orderId){
        buyerService.cancelOrder(openid, orderId);
        return ResultVo.success();
    }
}
